package com.yami.shop.consign.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.consign.common.dao.ConsignOptionalMapper;
import com.yami.shop.consign.common.model.ConsignOptional;
import com.yami.shop.consign.common.service.ConsignAnalysisService;
import com.yami.shop.consign.common.service.ConsignOptionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class ConsignOptionalServiceImpl extends ServiceImpl<ConsignOptionalMapper, ConsignOptional> implements ConsignOptionalService {

    @Autowired
    private ConsignAnalysisService consignAnalysisService;

    @Override
    public void addOrCancel(Integer prodId, String userId) {
        ConsignOptional one = getOne(new LambdaQueryWrapper<ConsignOptional>().eq(ConsignOptional::getProdId, prodId).eq(ConsignOptional::getUserId, userId));
        if (Objects.isNull(one)) {
            ConsignOptional optional = new ConsignOptional();
            optional.setProdId(prodId);
            optional.setUserId(userId);
            save(optional);
        } else {
            removeById(one.getId());
        }
        consignAnalysisService.updateHotspot(prodId);
    }
}
